<?php

$noberr="";
$nob="";

session_start();
include "dbconnect.php";
if(!isset($_SESSION['Username']))
{
  header("location:index.php");

}
else 
{
	if($_SESSION['Type']!=1)
  header("location:unauthorized.php");
	//echo isset($_SESSION['Username']);
	
}

//Variables to store usernme and opening
if (isset($_REQUEST["hireUsername"]))
{
	$_SESSION["hireUsername"]=$_REQUEST["hireUsername"];
	$_SESSION["openingid"]=$_REQUEST["courseid"];
}

else if($_SERVER["REQUEST_METHOD"] != "POST")
			{
 	header("location:hireView.php");
			}
//Code to get student details
$con=mysql_connect($dbHost,$dbUserAndName,$dbPass);
		if(!$con)
		{
			die("Error: ".mysql_error());
		}
		mysql_select_db($_DB_NAME,$con);
		
	$studentInfo=mysql_query("SELECT u.Firstname, u.Lastname,a.*, s . *,d.description,o.courseid
FROM students s, users u, appply a,degree d,openings o
WHERE a.openingid = " .$_SESSION["openingid"].
" AND u.username = '" .$_SESSION["hireUsername"].
"'  AND u.username=s.username  AND u.username = a.username and d.degreeid=s.Degree and o.openingid=a.openingid");
if($studentInfo == false)
{
	die(mysql_error() );}
	
	$post=0;
	$topreserve=0;
	
if($_SERVER["REQUEST_METHOD"]=="POST")
	{
		
		$topreserve=1;

		//After validation
		$hours=$_POST['hourstb']; 
		$contractType=$_POST["contractType"];
		
		
		//changed started
	
	if ($_POST["contractType"]=="HOURLY")
    	{
			 if(empty($hours))
				$noberr = "Hours is required";
				else
				{
					$nob = clean_input($hours);
					if (!preg_match("/^[0-9]*$/",$nob) or $nob>20)
				   {
						  $noberr = "Enter a number less than or equal to 20!";
						  
				   }
				   else
					{
						$post=1;
						$sql="INSERT INTO hire (username, openingid, contractid,hours)
						VALUES
						('".$_SESSION["hireUsername"]."',".$_SESSION["openingid"].",2,$hours)";
						
					}
				}
		}
			else
			{   
					  $post=1;//changed complete
				
						$sql="INSERT INTO hire (username, openingid, contractid)
						VALUES
						('".$_SESSION["hireUsername"]."',".$_SESSION["openingid"].",1)";
						

			}
						if($post==1)
						{
							if(!mysql_query($sql,$con))
							  {
							  die('Error:'.mysql_error().$_SESSION["hireUsername"] );
							  }
											  
							unset($_SESSION["hireUsername"]);
							unset($_SESSION["openingid"]);
							echo "<script>alert(\"Hired succesfully\")</script>";
							echo "<script>location.href = 'hireView.php?fromHire=1'</script>";
						}
					
			}
		
	function clean_input($data)
	{
	  $data = trim($data);
	  $data = stripslashes($data);
	  $data = htmlspecialchars($data);
	  return $data;
		}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Hire</title>

<script src="media/js/jquery.js" type="text/javascript"></script>
<script src="media/js/jquery.datatables.js" type="text/javascript"></script>

<link rel="stylesheet" href="jqwidgets/styles/jqx.base.css" type="text/css" />
    <link rel="stylesheet" href="jqwidgets/styles/jqx.summer.css" type="text/css" />
    <script type="text/javascript" src="scripts/jquery-2.0.3.min.js"></script>
    <script type="text/javascript" src="jqwidgets/jqxcore.js"></script>
    <script type="text/javascript" src="jqwidgets/jqxexpander.js"></script>

 

<script type="text/javascript" charset="utf-8">
$(document).ready(function () {
            // Create jqxExpander
            $("#studdetails").jqxExpander({});
        });
</script>

<script type="text/javascript" charset="utf-8">
$(document).ready(function () {
            // Create jqxExpander
            $("#hirediv").jqxExpander({});
        });
</script>


<script type="text/javascript" charset="utf-8">
$('#contractType').change( function () {
	alert("Here");
    var val = this.options[this.selectedIndex].value;
	
    document.getElementById("Hours").style.display = (val == "SAA") ? "block" : "none";  }
);​​​
</script>


<script>
function set(username)
{

	  window.location.href = 'hirestud.php?hireUsername='+ username;
   
	}
</script>
<!-- $hireUsername=$(this).parent(this).siblings("td:first").text();
    			$(this).leanModal({ top : 200, closeButton: ".modal_close" }); -->
					
<style type="text/css">
@import "media/css/demo_table_jui.css";
@import "media/themes/smoothness/jquery-ui-1.8.4.custom.css";
</style>
<link href="CSS/Global.css" type="text/css" rel="stylesheet" />
<link href="CSS/Base.css" type="text/css" rel="stylesheet" />
<link href="CSS/Fixed.css" type="text/css" rel="stylesheet" />
<style>
.hyperbutton {
    background:none;
     border:none; 
     padding:0;
    /*optional*/
    font-family:arial,sans-serif; /*input has OS specific font-family*/
     color:#990000;
     text-decoration:underline;
     cursor:pointer;
}

</style>

</head>

<body style="min-width:1000px">
<div id="branding-bar" class="gateway">
<div class="bar">
<div class="wrapper">
<h1 class="campus"><a><img  height="73" src="Resources/img/trident-tab.gif.png" width="64"/>AIMS</a></h1>
</div>
</div>
</div>
<div class="grey">
</div>
			<div id="hnav" style="background:none"><!--system-region name="GLOBAL NAVIGATION"/-->
              <ul>
              <li ><a href="hireView.php">Hire</a></li>
                <li ><a href="post.php">Openings</a></li>
                <li ><a href="budget.php">Budget</a></li>
                <li ><a href="semester.php">Semester</a></li>
                <li ><a href="manage.php">Manage</a></li>
              </ul>
      	</div>
        <span style="width:100px;margin-left:300px;">
                <button class="hyperbutton" onclick="location.href = 'logout.php'" >logout</button>
         </span>
<div class="footbody">
<div id="studdetails">
	<div>
	<?php 
     while($row=mysql_fetch_array($studentInfo)){
     echo $row["Firstname"]." ".$row["Lastname"] ."'s details";
	 ?>
     </div>
     <div>
     <table class="registrationtable" cellpadding="5" cellspacing="5">
     <tr>
         <td>
         <label >Degree</label>
         </td>
         <td>
         <label  class="crimson-text"><?php echo $row["description"]?></label>
         </td>
         <td>
         <label>Program</label>
         </td>
         <td>
         <label  class="crimson-text"><?php echo $row["Program"]?></label>
         </td>
     </tr>
     <tr>
         <td>
         <label>Year</label>
         </td>
         <td
         <label  class="crimson-text"><?php echo $row["Year"]?></label>
         </td>
         <td>
         <label >Email Id</label>
         </td>
         <td>
         <label  class="crimson-text"><?php echo $row["Emailid"]?></label>
         </td>
     </tr>
     <tr>
         <td>
         <label >GPA</label>
         </td>
         <td>
         <label  class="crimson-text"><?php echo $row["GPA"]?></label>
         </td>
         <td>
         <label >TEPAIC</label>
         </td>
         <td>
         <label  class="crimson-text"><?php echo $row["TEPAIC"]?></label>
         </td>
     </tr>
   	  <tr>
         <td>
         <label >Course</label>
         </td>
         <td>
         <label  class="crimson-text"><?php echo $row["courseid"]?></label>
         </td>
         <td>
         <label>Grade</label>
         </td>
         <td>
         <label  class="crimson-text"><?php echo $row["grade"]?></label>
         </td>
     </tr>
     <tr>
     <td>
     <label >Experience</label>
     </td>
     <td colspan="3">
     <label  class="crimson-text"><?php echo $row["Experience"]?></label>
     </td>
     </tr>
     </table>
     <?php }?>
     </div>
 </div>
	 <div id="hirediv" style="margin-top:20px;">
     	<div>
        Hiring Details
        </div>
        <div>
			<form method="post" action="<?php echo basename(htmlspecialchars($_SERVER["PHP_SELF"]));?>" >
          <table >
          
          <tbody>
          
          <tr>
			<td>
            <label class="crimson-text">Select a contract type</label>
            </td>
              <td>
              <select name="contractType" class="crimson-border">
              <option value="SAA" <?php if($topreserve){ if($contractType=="SAA") echo "selected"; }?> >SAA</option>
              <option value="HOURLY" <?php if($topreserve){ if($contractType=="HOURLY") echo "selected"; } ?> >Hourly </option>
              </select>
              </td>    
          <tr/>
          
          <tr>
              <td>
              <label class="crimson-text">Number of Hours</label>
              </td>
              <td>
              <input type="text" name="hourstb" class="crimson-border" value="<?php echo $nob;?>" />
            </td>
              <td class="error">
    * <?php echo $noberr;?>
        </td>
              </div>
              </td>
              
          <tr/>
          
          <tr>
               <td rowspan="2">
              <input  type="submit" name="assignsubmit" value="Hire"></button>
               </td>    
           </tr>
           </tbody>
          </table>
          
          </form>
          </div>
          </div>
          </div>
    </body>
</html>
